home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gsht.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-21  |  16.5 KB  |  553 lines

  1. /* Copyright (C) 1989, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gsht.c */
  20. /* setscreen operator for Ghostscript library */
  21. #include "memory_.h"
  22. #include <stdlib.h>        /* for qsort */
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsstruct.h"
  26. #include "gsutil.h"        /* for gs_next_ids */
  27. #include "gxarith.h"        /* for igcd */
  28. #include "gzstate.h"
  29. #include "gxdevice.h"            /* for gzht.h */
  30. #include "gzht.h"
  31.  
  32. /* Forward declarations */
  33. void gx_set_effective_transfer(P1(gs_state *));
  34.  
  35. /* Structure types */
  36. public_st_ht_order();
  37. private_st_ht_order_component();
  38. public_st_ht_order_comp_element();
  39. public_st_halftone();
  40. public_st_device_halftone();
  41.  
  42. /* GC procedures */
  43.  
  44. #define hptr ((gs_halftone *)vptr)
  45.  
  46. private ENUM_PTRS_BEGIN(halftone_enum_ptrs) return 0;
  47.     case 0:
  48.         switch ( hptr->type )
  49.         {
  50.         case ht_type_threshold:
  51.           ENUM_RETURN_CONST_STRING_PTR(gs_halftone, params.threshold.thresholds);
  52.         case ht_type_multiple:
  53.         case ht_type_multiple_colorscreen:
  54.           ENUM_RETURN(hptr->params.multiple.components);
  55.         default:
  56.           return 0;
  57.         }
  58. ENUM_PTRS_END
  59.  
  60. private RELOC_PTRS_BEGIN(halftone_reloc_ptrs) {
  61.     switch ( hptr->type )
  62.       {
  63.       case ht_type_threshold:
  64.         RELOC_CONST_STRING_PTR(gs_halftone, params.threshold.thresholds);
  65.         break;
  66.       case ht_type_multiple:
  67.       case ht_type_multiple_colorscreen:
  68.         RELOC_PTR(gs_halftone, params.multiple.components);
  69.         break;
  70.       case ht_type_none:
  71.       case ht_type_screen:
  72.       case ht_type_colorscreen:
  73.       case ht_type_spot:
  74.         break;
  75.       }
  76. } RELOC_PTRS_END
  77.  
  78. #undef hptr
  79.  
  80. /* setscreen */
  81. int
  82. gs_setscreen(gs_state *pgs, gs_screen_halftone *phsp)
  83. {    gs_screen_enum senum;
  84.     int code = gx_ht_process_screen(&senum, pgs, phsp,
  85.                     gs_currentaccuratescreens());
  86.     if ( code < 0 )
  87.         return code;
  88.     return gs_screen_install(&senum);
  89. }
  90.  
  91. /* currentscreen */
  92. int
  93. gs_currentscreen(const gs_state *pgs, gs_screen_halftone *phsp)
  94. {    switch ( pgs->halftone->type )
  95.     {
  96.     case ht_type_screen:
  97.         *phsp = pgs->halftone->params.screen;
  98.         return 0;
  99.     case ht_type_colorscreen:
  100.         *phsp = pgs->halftone->params.colorscreen.screens.colored.gray;
  101.         return 0;
  102.     default:
  103.         return_error(gs_error_undefined);
  104.     }
  105. }
  106.  
  107. /* .currentscreenlevels */
  108. int
  109. gs_currentscreenlevels(const gs_state *pgs)
  110. {    return pgs->dev_ht->order.num_levels;
  111. }
  112.  
  113. /* .setscreenphase */
  114. int
  115. gx_imager_setscreenphase(gs_imager_state *pis, int x, int y,
  116.   gs_color_select_t select)
  117. {    if ( select == gs_color_select_all )
  118.       { int i;
  119.         for ( i = 0; i < gs_color_select_count; ++i )
  120.           gx_imager_setscreenphase(pis, x, y, (gs_color_select_t)i);
  121.         return 0;
  122.       }
  123.     else if ( select < 0 || select >= gs_color_select_count )
  124.       return_error(gs_error_rangecheck);
  125.     pis->screen_phase[select].x = x;
  126.     pis->screen_phase[select].y = y;
  127.     return 0;
  128. }
  129. int
  130. gs_setscreenphase(gs_state *pgs, int x, int y, gs_color_select_t select)
  131. {    int code = gx_imager_setscreenphase((gs_imager_state *)pgs, x, y,
  132.                         select);
  133.  
  134.     /*
  135.      * If we're only setting the source phase, we don't need to do
  136.      * unset_dev_color, because the source phase doesn't affect painting
  137.      * with the current color.
  138.      */
  139.     if ( code >= 0 && (select == gs_color_select_texture ||
  140.                select == gs_color_select_all)
  141.        )
  142.       gx_unset_dev_color(pgs);
  143.     return code;
  144. }
  145.  
  146. /* .currentscreenphase */
  147. int
  148. gs_currentscreenphase(const gs_state *pgs, gs_int_point *pphase,
  149.   gs_color_select_t select)
  150. {    if ( select < 0 || select >= gs_color_select_count )
  151.       return_error(gs_error_rangecheck);
  152.     *pphase = pgs->screen_phase[select];
  153.     return 0;
  154. }
  155.  
  156. /* currenthalftone */
  157. int
  158. gs_currenthalftone(gs_state *pgs, gs_halftone *pht)
  159. {    *pht = *pgs->halftone;
  160.     return 0;
  161. }
  162.  
  163. /* ------ Internal routines ------ */
  164.  
  165. /* Process one screen plane. */
  166. int
  167. gx_ht_process_screen_memory(gs_screen_enum *penum, gs_state *pgs,
  168.   gs_screen_halftone *phsp, bool accurate, gs_memory_t *mem)
  169. {    gs_point pt;
  170.     int code = gs_screen_init_memory(penum, pgs, phsp, accurate, mem);
  171.  
  172.     if ( code < 0 ) return code;
  173.     while ( (code = gs_screen_currentpoint(penum, &pt)) == 0 )
  174.       if ( (code = gs_screen_next(penum, (*phsp->spot_function)(pt.x, pt.y))) < 0 )
  175.         return code;
  176.     return 0;
  177. }
  178.  
  179. /* Allocate and initialize the contents of a halftone order. */
  180. /* The client must have set the defining values in porder->params. */
  181. int
  182. gx_ht_alloc_order(gx_ht_order *porder, uint width, uint height,
  183.   uint strip_shift, uint num_levels, gs_memory_t *mem)
  184. {    uint size = width * height;
  185.     gx_ht_order order;
  186.  
  187.     order = *porder;
  188.     gx_compute_cell_values(&order.params);
  189.     order.width = width;
  190.     order.height = height;
  191.     order.raster = bitmap_raster(width);
  192.     order.shift = strip_shift;
  193.     order.orig_height = order.height;
  194.     order.orig_shift = order.shift;
  195.     order.full_height = ht_order_full_height(&order);
  196.     order.num_levels = num_levels;
  197.     order.num_bits = size;
  198.     order.levels =
  199.       (uint *)gs_alloc_byte_array(mem, num_levels, sizeof(uint),
  200.                       "ht order(levels)");
  201.     order.bits =
  202.       (gx_ht_bit *)gs_alloc_byte_array(mem, size, sizeof(gx_ht_bit),
  203.                        "ht order(bits)");
  204.     if ( order.levels == 0 || order.bits == 0 )
  205.     {    gs_free_object(mem, order.bits, "ht order(bits)");
  206.         gs_free_object(mem, order.levels, "ht order(levels)");
  207.         return_error(gs_error_VMerror);
  208.     }
  209.     order.cache = 0;
  210.     order.transfer = 0;
  211.     *porder = order;
  212.     return 0;
  213. }
  214.  
  215. /* Compare keys ("masks", actually sample values) for qsort. */
  216. private int
  217. compare_samples(const void *p1, const void *p2)
  218. {    ht_sample_t m1 = ((const gx_ht_bit *)p1)->mask;
  219.     ht_sample_t m2 = ((const gx_ht_bit *)p2)->mask;
  220.     return (m1 < m2 ? -1 : m1 > m2 ? 1 : 0);
  221. }
  222. /* Sort the halftone order by sample value. */
  223. void
  224. gx_sort_ht_order(gx_ht_bit *recs, uint N)
  225. {    int i;
  226.     /* Tag each sample with its index, for sorting. */
  227.     for ( i = 0; i < N; i++ )
  228.       recs[i].offset = i;
  229.     qsort((void *)recs, N, sizeof(*recs), compare_samples);
  230. #ifdef DEBUG
  231. if ( gs_debug_c('H') )
  232.     {    uint i;
  233.         dprintf("[H]Sorted samples:\n");
  234.         for ( i = 0; i < N; i++ )
  235.             dprintf3("%5u: %5u: %u\n",
  236.                  i, recs[i].offset, recs[i].mask);
  237.     }
  238. #endif
  239. }
  240.  
  241. /*
  242.  * Construct the halftone order from a sampled spot function.  Only width x
  243.  * strip samples have been filled in; we must replicate the resulting sorted
  244.  * order vertically, shifting it by shift each time.  See gxdht.h regarding
  245.  * the invariants that must be restored.
  246.  */
  247. void
  248. gx_ht_construct_spot_order(gx_ht_order *porder)
  249. {    uint width = porder->width;
  250.     uint height = porder->orig_height;
  251.     uint num_levels = porder->num_levels;    /* = width x strip */
  252.     uint strip = num_levels / width;
  253.     gx_ht_bit *bits = porder->bits;
  254.     uint *levels = porder->levels;
  255.     uint shift = porder->orig_shift;
  256.     uint full_height = porder->full_height;
  257.     uint num_bits = porder->num_bits;
  258.     uint copies = num_bits / (width * strip);
  259.     gx_ht_bit *bp = bits + num_bits - 1;
  260.     uint i;
  261.  
  262.     gx_sort_ht_order(bits, num_levels);
  263.     if_debug5('h',
  264.           "[h]spot order: num_levels=%u w=%u h=%u strip=%u shift=%u\n",
  265.           num_levels, width, height, strip, shift);
  266.     /* Fill in the levels array, replicating the bits vertically */
  267.     /* if needed. */
  268.     for ( i = num_levels; i > 0; )
  269.       { uint offset = bits[--i].offset;
  270.         uint x = offset % width;
  271.         uint hy = offset - x;
  272.         uint k;
  273.  
  274.         levels[i] = i * copies;
  275.         for ( k = 0; k < copies;
  276.           k++, bp--, hy += num_levels, x = (x + width - shift) % width
  277.         )
  278.           bp->offset = hy + x;
  279.       }
  280.     /* If we have a complete halftone, restore the invariant. */
  281.     if ( num_bits == width * full_height )
  282.       { porder->height = full_height;
  283.         porder->shift = 0;
  284.       }
  285.     gx_ht_construct_bits(porder);
  286. }
  287.  
  288. /* Construct offset/masks from the whitening order. */
  289. /* porder->bits[i].offset contains the index of the bit position */
  290. /* that is i'th in the whitening order. */
  291. void
  292. gx_ht_construct_bits(gx_ht_order *porder)
  293. {    uint width = porder->width;
  294.     uint size = porder->num_bits;
  295.     gx_ht_bit *bits = porder->bits;
  296.     uint i;
  297.     gx_ht_bit *phb;
  298.     byte *pb;
  299.     uint padding = porder->raster * 8 - width;
  300.  
  301.     for ( i = 0, phb = bits; i < size; i++, phb++ )
  302.     {    int pix = phb->offset;
  303.         ht_mask_t mask;
  304.         pix += pix / width * padding;
  305.         phb->offset = (pix >> 3) & -size_of(mask);
  306.         mask = (ht_mask_t)1 << (~pix & (ht_mask_bits - 1));
  307.         /* Replicate the mask bits. */
  308.         pix = ht_mask_bits - width;
  309.         while ( (pix -= width) >= 0 )
  310.             mask |= mask >> width;
  311.         /* Store the mask, reversing bytes if necessary. */
  312.         phb->mask = 0;
  313.         for ( pb = (byte *)&phb->mask + (sizeof(mask) - 1);
  314.               mask != 0;
  315.               mask >>= 8, pb--
  316.             )
  317.             *pb = (byte)mask;
  318.     }
  319. #ifdef DEBUG
  320. if ( gs_debug_c('H') )
  321.        {    dprintf1("[H]Halftone order bits 0x%lx:\n", (ulong)bits);
  322.         for ( i = 0, phb = bits; i < size; i++, phb++ )
  323.             dprintf3("%4d: %u:0x%lx\n", i, phb->offset,
  324.                  (ulong)phb->mask);
  325.        }
  326. #endif
  327. }
  328.  
  329. /* Release a gx_device_halftone by freeing its components. */
  330. /* (Don't free the gx_device_halftone itself.) */
  331. void
  332. gx_ht_order_release(gx_ht_order *porder, gs_memory_t *mem, bool free_cache)
  333. {    if ( free_cache && porder->cache )
  334.       gx_ht_free_cache(mem, porder->cache);
  335.     gs_free_object(mem, porder->transfer, "gx_ht_order_release(transfer)");
  336.     gs_free_object(mem, porder->bits, "gx_ht_order_release(bits)");
  337.     gs_free_object(mem, porder->levels, "gx_ht_order_release(levels)");
  338. }
  339. void
  340. gx_device_halftone_release(gx_device_halftone *pdht, gs_memory_t *mem)
  341. {    if ( pdht->components )
  342.       { int i;
  343.         /* One of the components might be the same as the default */
  344.         /* order, so check that we don't free it twice. */
  345.         for ( i = 0; i < pdht->num_comp; ++i )
  346.           if ( pdht->components[i].corder.bits !=
  347.            pdht->order.bits
  348.          )
  349.         { /* Currently, all orders except the default one */
  350.           /* own their caches. */
  351.           gx_ht_order_release(&pdht->components[i].corder, mem, true);
  352.         }
  353.         gs_free_object(mem, pdht->components,
  354.                "gx_dev_ht_release(components)");
  355.         pdht->components = 0;
  356.         pdht->num_comp = 0;
  357.       }
  358.     gx_ht_order_release(&pdht->order, mem, false);
  359. }
  360.  
  361. /* Install a device halftone in an imager state. */
  362. /* Note that this does not read or update the client halftone. */
  363. int
  364. gx_imager_dev_ht_install(gs_imager_state *pis,
  365.   const gx_device_halftone *pdht, gs_halftone_type type, const gx_device *dev)
  366. {    gx_device_halftone *pgdht = pis->dev_ht;
  367.  
  368.     if ( (ulong)pdht->order.raster * (pdht->order.num_bits /
  369.            pdht->order.width) > pis->ht_cache->bits_size
  370.        )
  371.       return_error(gs_error_limitcheck);
  372.     if ( pgdht != 0 && pgdht->rc.ref_count == 1 &&
  373.          pgdht->rc.memory == pdht->rc.memory
  374.        )
  375.       { /* The current device halftone isn't shared. */
  376.         /* Just release its components. */
  377.         gx_device_halftone_release(pgdht, pgdht->rc.memory);
  378.       }
  379.     else
  380.       { /* The device halftone is shared or not yet allocated. */
  381.         rc_unshare_struct(pis->dev_ht, gx_device_halftone,
  382.                   &st_device_halftone, pdht->rc.memory,
  383.                   return_error(gs_error_VMerror),
  384.                   "gx_imager_dev_ht_install");
  385.         pgdht = pis->dev_ht;
  386.       }
  387.     { rc_header rc;
  388.       rc = pgdht->rc;
  389.       *pgdht = *pdht;
  390.       pgdht->rc = rc;
  391.     }
  392.     pgdht->id = gs_next_ids(1);
  393. #ifdef FUTURE
  394.     pgdht->type = type;
  395. #endif
  396.     /* Clear the cache, to avoid confusion in case the address of */
  397.     /* a new order vector matches that of a (deallocated) old one. */
  398.     gx_ht_clear_cache(pis->ht_cache);
  399.     /* Set the color_indices according to the device color_info. */
  400.     /* Also compute the LCM of the primary color cell sizes. */
  401.     /* Note that for strip halftones, the "cell size" is the */
  402.     /* theoretical fully expanded size with shift = 0. */
  403.     if ( pdht->components != 0 )
  404.     {    static const gs_ht_separation_name dcnames[5][4] =
  405.         {    { gs_ht_separation_Default },    /* not used */
  406.             { gs_ht_separation_Default, gs_ht_separation_Default,
  407.               gs_ht_separation_Default, gs_ht_separation_Gray
  408.             },
  409.             { gs_ht_separation_Default },    /* not used */
  410.             { gs_ht_separation_Red, gs_ht_separation_Green,
  411.               gs_ht_separation_Blue, gs_ht_separation_Default
  412.             },
  413.             { gs_ht_separation_Cyan, gs_ht_separation_Magenta,
  414.               gs_ht_separation_Yellow, gs_ht_separation_Black
  415.             }
  416.         };
  417.         static const gs_ht_separation_name cscnames[4] =
  418.             { gs_ht_separation_Red, gs_ht_separation_Green,
  419.               gs_ht_separation_Blue, gs_ht_separation_Default
  420.             };
  421.         int num_comps = dev->color_info.num_components;
  422.         const gs_ht_separation_name _ds *cnames = dcnames[num_comps];
  423.         int lcm_width = 1, lcm_height = 1;
  424.         uint i;
  425.  
  426.         /* Halftones set by setcolorscreen, and (we think) */
  427.         /* Type 2 and Type 4 halftones, are supposed to work */
  428.         /* for both RGB and CMYK, so we need a special check here. */
  429.         if ( num_comps == 4 &&
  430.              (type == ht_type_colorscreen ||
  431.               type == ht_type_multiple_colorscreen)
  432.            )
  433.           cnames = cscnames;
  434.         if_debug4('h', "[h]dcnames=%lu,%lu,%lu,%lu\n",
  435.               (ulong)cnames[0], (ulong)cnames[1],
  436.               (ulong)cnames[2], (ulong)cnames[3]);
  437.         memset(pgdht->color_indices, 0, sizeof(pdht->color_indices));
  438.         for ( i = 0; i < pdht->num_comp; i++ )
  439.         {    const gx_ht_order_component *pcomp =
  440.               &pdht->components[i];
  441.             int j;
  442.             if_debug2('h', "[h]cname[%d]=%lu\n",
  443.                   i, (ulong)pcomp->cname);
  444.             for ( j = 0; j < 4; j++ )
  445.             {    if ( pcomp->cname == cnames[j] )
  446.                   { if_debug2('h', "[h]color_indices[%d]=%d\n",
  447.                           j, i);
  448.                     pgdht->color_indices[j] = i;
  449.                   }
  450.             }
  451.         }
  452.         /* Now do a second pass to compute the LCM. */
  453.         /* We have to do it this way in case some entry in */
  454.         /* color_indices is still 0. */
  455.         for ( i = 0; i < 4; ++i )
  456.           {    const gx_ht_order_component *pcomp =
  457.               &pdht->components[pgdht->color_indices[i]];
  458.             uint cw = pcomp->corder.width;
  459.             uint ch = pcomp->corder.full_height;
  460.             int dw = lcm_width / igcd(lcm_width, cw);
  461.             int dh = lcm_height / igcd(lcm_height, ch);
  462.             lcm_width = (cw > max_int / dw ? max_int : cw * dw);
  463.             lcm_height = (ch > max_int / dh ? max_int : ch * dh);
  464.           }
  465.         pgdht->lcm_width = lcm_width;
  466.         pgdht->lcm_height = lcm_height;
  467.     }
  468.     else
  469.       {    /* Only one component. */
  470.         pgdht->lcm_width = pgdht->order.width;
  471.         pgdht->lcm_height = pgdht->order.full_height;
  472.       }
  473.     if_debug2('h', "[h]LCM=(%d,%d)\n",
  474.           pgdht->lcm_width, pgdht->lcm_height);
  475.     gx_imager_set_effective_xfer(pis);
  476.     return 0;
  477. }
  478.  
  479. /*
  480.  * Install a new halftone in the graphics state.  Note that we copy the top
  481.  * level of the gs_halftone and the gx_device_halftone, and take ownership
  482.  * of any substructures.
  483.  */
  484. int
  485. gx_ht_install(gs_state *pgs, const gs_halftone *pht,
  486.   const gx_device_halftone *pdht)
  487. {    gs_memory_t *mem = pht->rc.memory;
  488.     gs_halftone *old_ht = pgs->halftone;
  489.     gs_halftone *new_ht;
  490.     int code;
  491.  
  492.     if ( old_ht != 0 && old_ht->rc.memory == mem &&
  493.          old_ht->rc.ref_count == 1
  494.        )
  495.       new_ht = old_ht;
  496.     else
  497.       rc_alloc_struct_1(new_ht, gs_halftone, &st_halftone,
  498.                 mem, return_error(gs_error_VMerror),
  499.                 "gx_ht_install(new halftone)");
  500.     code = gx_imager_dev_ht_install((gs_imager_state *)pgs,
  501.             pdht, pht->type, gs_currentdevice_inline(pgs));
  502.     if ( code < 0 )
  503.       { if ( new_ht != old_ht )
  504.           gs_free_object(mem, new_ht, "gx_ht_install(new halftone)");
  505.         return code;
  506.       }
  507.     if ( new_ht != old_ht )
  508.       rc_decrement(old_ht, "gx_ht_install(old halftone)");
  509.     { rc_header rc;
  510.       rc = new_ht->rc;
  511.       *new_ht = *pht;
  512.       new_ht->rc = rc;
  513.     }
  514.     pgs->halftone = new_ht;
  515.     gx_unset_dev_color(pgs);
  516.     return 0;
  517. }
  518.  
  519. /* Reestablish the effective transfer functions, taking into account */
  520. /* any overrides from halftone dictionaries. */
  521. void
  522. gx_imager_set_effective_xfer(gs_imager_state *pis)
  523. {    const gx_device_halftone *pdht = pis->dev_ht;
  524.  
  525.     pis->effective_transfer = pis->set_transfer;        /* default */
  526.     if ( pdht == 0 )
  527.       return;        /* not initialized yet */
  528.     if ( pdht->components == 0 )
  529.       {    /* Check for transfer function override in single halftone */
  530.         gx_transfer_map *pmap = pdht->order.transfer;
  531.         if ( pmap != 0 )
  532.           pis->effective_transfer.indexed[0] =
  533.             pis->effective_transfer.indexed[1] =
  534.             pis->effective_transfer.indexed[2] =
  535.             pis->effective_transfer.indexed[3] = pmap;
  536.       }
  537.     else
  538.       {    /* Check in all 4 standard separations */
  539.         int i;
  540.         for ( i = 0; i < 4; ++i )
  541.           {    gx_transfer_map *pmap =
  542.               pdht->components[pdht->color_indices[i]].corder.
  543.                 transfer;
  544.             if ( pmap != 0 )
  545.               pis->effective_transfer.indexed[i] = pmap;
  546.           }
  547.       }
  548. }
  549. void
  550. gx_set_effective_transfer(gs_state *pgs)
  551. {    gx_imager_set_effective_xfer((gs_imager_state *)pgs);
  552. }
  553.